/**
 * Encrypt 
 * 加密
 * 2008-02-27
 * @version where 0.2
 * @author  Shea
 */
package zozopo.comm;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Encrypt {
	private static final int[] encryptKey       = {1,3,6,7,2,0,2,4,8,9,4,2,2,3,4,5,8,1,6,3};//勿加入太多 或 加入大于10的数! 
	private static final int   encryptKeyLength = encryptKey.length;
	private static final int   encryptCtr       = encryptKey[encryptKeyLength-1];
	
	
	
	public static String MessageDigest(String s){
		try {
			byte[] strTemp = s.getBytes();
			
			MessageDigest md = MessageDigest.getInstance("MD5"); 
			
			md.update(strTemp); 
			byte b[] = md.digest(); 

			int i; 
			int iKey = 0;
			
			StringBuffer buf = new StringBuffer(""); 
			for (int offset = 0; offset < b.length; offset++) { 
			i = b[offset]; 
			
			//Shea			
			iKey = encryptCtr(encryptKeyLength,encryptCtr,iKey);
			i += encryptKey[iKey-1];
			
			if(i<0) {i+= 256;}
			if(i<16){buf.append("0"); }
			
			buf.append(Integer.toHexString(i)); 
			} 
			s = buf.toString();
			
			//System.out.println("result: " + buf.toString());//32位的加密 
			//System.out.println("result: " + buf.toString().substring(8,24));//16位的加密 

			
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		return s;
	}
	
	//移动encryptKey 位数
	private static int encryptCtr(int encryptKeyLength,int encryptCtr,int iKey){
		//System.out.println(encryptKeyLength+","+encryptCtr+","+iKey);
		if((iKey + encryptCtr) < (encryptKeyLength-1)){
			return (iKey + encryptCtr);
		}else{
			encryptCtr(encryptKeyLength,encryptCtr,(iKey-encryptKeyLength));
		}
		return iKey;
	}
	
}